<?php

namespace App\Models\User;

use App\Models\Cert\CertDriver;
use App\Models\Cert\CertGo;
use App\Models\Cert\CertGoOther;
use App\Models\Common\CommonModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;

class User extends CommonModel
{
    use HasFactory,SoftDeletes;
    protected $table = 'user';
    protected $appends = ['is_finish_wx'];

    public function getIsFinishWxAttribute() {
        return !(empty($this->wx_nickname) || empty($this->wx_avatar) );
    }

    public function getRecords(array $conditions) {


        if(!isset($conditions['orderBy'])){
            $conditions['orderBy']['col'] = 'id';
            $conditions['orderBy']['sort'] = 'desc';
        }

        $records = $this->newQuery();
        if(isset($conditions['ids'])){
            if(is_array($conditions['ids'])){
                $conditions['ids'] = array_filter($conditions['ids']);
                //这一层判断必须有，当array_filter是一个空数组
                if(count($conditions['ids'])>0){
                    $records = count($conditions['ids'])>1?$records->whereIn('id',$conditions['ids']):
                        $records->where('id',Arr::first($conditions['ids']));
                }
            }
            else{
                $records = $records->where('id',$conditions['ids']);
            }
        }

        if(!empty($conditions['password'])){
            $records = $records->where('password',$conditions['password']);
        }
        if(!empty($conditions['phone'])){
            $records = $records->where('phone',$conditions['phone']);
        }
        if(!empty($conditions['isBinding'])){
            if($conditions['isBinding']==1){
                $records = $records->where('serial_number','!=',null);
            }elseif($conditions['isBinding']==2){
                $records = $records->where('serial_number',null);
            }else{

            }
        }
        if(!empty($conditions['createdAt'])){
            $records = $records->where('created_at','>=',$conditions['createdAt'][0])
                ->where('created_at','<=',$conditions['createdAt'][1]);
        }
        if(!empty($conditions['activeTime'])){
            $records = $records->where('active_time','>=',$conditions['activeTime'][0])
                ->where('active_time','<=',$conditions['activeTime'][1]);
        }
        if(!empty($conditions['name'])){
            $records = $records->whereHas('getDriverInfo', function ($q) use ($conditions) {
               $q->where('name','like','%'.$conditions['name'].'%');
            });
        }

        return $records->orderBy($conditions['orderBy']['col'],$conditions['orderBy']['sort']);
    }


    public function getDriverInfo() {
        return $this->hasOne(CertDriver::class,'user_id','id');
    }

    public function getGo() {
        return $this->hasOne(CertGo::class,'user_id','id');
    }

    public function getGoOther() {
        return $this->hasOne(CertGoOther::class,'user_id','id');
    }
}
